/*
 * $Id: HibernateUtil.java 32 2007-04-18 00:31:38Z aaron.day $
 */
package org.crazydays.hibernate;


import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.HibernateException;
import org.hibernate.cfg.AnnotationConfiguration;

import org.crazydays.core.data.PolisData;

/**
 * HibernateUtil
 */
public class HibernateUtil
{
	/** session factory */
	private static final SessionFactory sessionFactory;

	// initialize session factory
	static {
		try {
			sessionFactory = new AnnotationConfiguration()
					.configure().buildSessionFactory();
		} catch (Throwable e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	/**
	 * Get hibernate session.
	 * 
	 * @return Session
	 * @throws HibernateException
	 */
	public static Session getSession()
		throws HibernateException
	{
		return sessionFactory.openSession();
	}

	/**
	 * Load object.
	 * 
	 * @param session Session
	 * @param clazz Class
	 * @param id Id
	 * @return Object
	 * @throws HibernateException
	 */
	public static Object loadObject(Session session, Class clazz,
			Serializable id)
		throws HibernateException
	{
		if (session == null) {
			throw new IllegalArgumentException("session == null");
		}
		if (clazz == null) {
			throw new IllegalArgumentException("clazz == null");
		}
		if (id == null) {
			throw new IllegalArgumentException("id == null");
		}

		return session.load(clazz, id);
	}

	/**
	 * Load polis data by name.
	 * 
	 * @param clazz Class
	 * @param name Name
	 * @return Object
	 * @throws HibernateException
	 */
	public static PolisData loadPolisDataByName(Class clazz, String name)
		throws HibernateException
	{
		Session session = null;
		try {
			session = getSession();

			return loadPolisDataByName(session, clazz, name);
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	/**
	 * Load polis data by name.
	 * 
	 * @param session Hibernate session
	 * @param clazz Class
	 * @param name Name
	 * @return PolisData
	 * @throws HibernateException
	 */
	public static PolisData loadPolisDataByName(Session session,
			Class clazz, String name)
		throws HibernateException
	{
		if (session == null) {
			throw new IllegalArgumentException("session == null");
		}
		if (clazz == null) {
			throw new IllegalArgumentException("clazz == null");
		}
		if (name == null) {
			throw new IllegalArgumentException("name == null");
		}

		StringBuffer hql = new StringBuffer();

		hql.append("FROM ");
		hql.append(clazz.getName());
		hql.append(" WHERE name = ?");

		Query query = session.createQuery(hql.toString());
		query.setString(0, name);

		List results = query.list();
		if (results.size() == 0) {
			return null;
		} else if (results.size() == 1) {
			return (PolisData) results.get(0);
		} else {
			throw new HibernateException("Multiple results "
					+ results.size());
		}
	}

	/**
	 * Save polis data.
	 * 
	 * @param session Session
	 * @param data Data
	 * @throws HibernateException
	 */
	public static void savePolisData(Session session, PolisData data)
		throws HibernateException
	{
		if (session == null) {
			throw new IllegalArgumentException("session == null");
		}
		if (data == null) {
			throw new IllegalArgumentException("data == null");
		}

		session.save(data);
	}

	/**
	 * Save or update polis data.
	 * 
	 * @param session Session
	 * @param data Data
	 * @throws HibernateException
	 */
	public static void saveOrUpdatePolisData(Session session, PolisData data)
		throws HibernateException
	{
		if (session == null) {
			throw new IllegalArgumentException("session == null");
		}
		if (data == null) {
			throw new IllegalArgumentException("data == null");
		}

		session.saveOrUpdate(data);
	}

	/**
	 * Delete polis data.
	 * 
	 * @param session Session
	 * @param data Data
	 * @throws HibernateException
	 */
	public static void deletePolisData(Session session, PolisData data)
		throws HibernateException

	{
		if (session == null) {
			throw new IllegalArgumentException("session == null");
		}
		if (data == null) {
			throw new IllegalArgumentException("data == null");
		}

		session.delete(data);
	}
}
